#include "modgroupdb.h"

int main(int argc, char **argv) {
  CONNECTION_DB conn = NULL;
  struct group_t * groupt;
  char sql[512];
  PGresult *result;
  int c;
  
  groupt = (struct group_t *) malloc(sizeof(struct group_t));
  groupt->name=NULL;
  groupt->group_id=-1;

  if(argc < 2) {
    print_help();
    exit(0);
  }

  while ( (c=getopt (argc, argv, "g:n:h")) != -1 ) {
    
    switch (c) {
     
    case 'h':
      print_help();
      exit(0);
      break;

    case 'g':
      groupt->group_id = atoi(optarg);
      break;
      
    case 'n':
      groupt->name = strdup(optarg);
      break;
      
    case '?':
      break;
      
    }
    
  }
  char * current_name=NULL;
  if(optind < argc) {
    current_name = strdup(argv[optind++]);
  } else {
    printf("\n     >> Grupo nao especificado << \n\n");
    print_help();
    exit(0);
  }
  
  if( current_name == NULL) {
    printf("\n     >> Grupo nao especificado<< \n\n");
    print_help();
    exit(0);
  }

  if(getuid() != 0) {
    printf("Somente o root pode executar esse comando\n");
    exit(0);
  }
  
  conn = init_db(DB_ARQ_CONF);	
  if( PQstatus(conn->pgconn) != CONNECTION_OK ) {
    printf("Falha na conexao\n");
    exit(0);
  }

  int current_group_id;

  sprintf(sql, "SELECT group_id FROM group_sys WHERE name='%s'", current_name);
  result = PQexec(conn->pgconn, sql);
  if(PQntuples(result)<=0) {
    printf("Grupo nao existe\n");
    PQfinish(conn->pgconn);
    exit(0);
  }
  else{
    current_group_id=atoi(PQgetvalue(result,0,0));
  }
  
  if(groupt->group_id!=-1){
    sprintf(sql, "SELECT group_id FROM group_sys WHERE group_id=%d", groupt->group_id);
    result = PQexec(conn->pgconn,sql);
    if (PQntuples(result)>0){
      printf("There is one group with gid = %d", groupt->group_id);
      exit(0);
    }
  }
  
  if(groupt->name!=NULL){
    sprintf(sql, "SELECT name FROM group_sys WHERE name='%s'", groupt->name);
    result = PQexec(conn->pgconn,sql);
    if (PQntuples(result)>0){
      printf("There is already one group with name = %s", groupt->name);
      exit(0);
    }
  }

  char * update_name;
  int update_id;
  if (groupt->name!=NULL)
    update_name=groupt->name;
  else
    update_name=current_name;
  
  if (groupt->group_id!=-1)
    update_id=groupt->group_id;
  else
    update_id=current_group_id;
  
  printf("This operations modify the group %s\n", current_name);
  printf("New group name: %s\n", update_name);
  printf("New GID: %d\n", update_id);

  printf("Is data correct? [y/n] ");
  while( (c=getchar()) != 'y' && c != 'n' ) {
    printf("Is data corret? [y/n] ");
    getchar();
  }
  if(c=='n') exit(0);
    
  sprintf(sql, "UPDATE group_sys SET group_id=%d, name='%s' WHERE group_id=%d",
	  update_id, update_name, current_group_id);
  
  if(PQexec(conn->pgconn, sql)==NULL){
    printf("Erro\n");
  } else {
    printf("Group updated \n");
    /*
    int pid = fork();
    if (pid == 0){
      execlp(GROUP_CREAT, NULL,NULL);
    }
    */
  }

  PQclear(result);
  free_db(conn);
  return 0;

}

void print_help() {

  printf("Uso correto: delgroupdb group\n\n");
  printf("  -n          Novo nome do grupo\n");
  printf("  -g          Novo ID do grupo\n");
  printf("  -h		Mostra essa ajuda\n");
  printf("\nSECURE DB USERS SYSTEM MANAGEMENT\n");

}



